package edu.tyut.game.ui.game

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import edu.tyut.game.R
import edu.tyut.game.logic.model.Game
import edu.tyut.game.utils.LogUtils

class GameAdapter(private val context: Context, private val gameList: List<Game>): RecyclerView.Adapter<GameAdapter.ViewHolder>(){

    private val tag = "GameAdapter"

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
        val gameImage: ImageView = view.findViewById(R.id.gameImage)
        val gameName: TextView = view.findViewById(R.id.gameName)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.game_item, parent, false)
        val holder = ViewHolder(view)
        holder.itemView.setOnClickListener {
            LogUtils.d(tag, "我想玩游戏！")
            FlowerActivity.actionStart(context)
        }
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val game = gameList[position]
        holder.gameName.text = game.name
        Glide.with(context).load(game.imageId).into(holder.gameImage)
    }

    override fun getItemCount() = gameList.size
}